/* (PARI) R. J. Cano, Jun 19 2014;
 *
 * "Watchdog" or "Test counters" version (Formerly for debugging purposes).
 * 
 * Note from the author: "I am NOT enough good in English (unfortunately)
 *                         to give a more clear, deep, and technical explanation
 *                          about the sieve-completion algorithm, however a little
 *                           bit of arithmetic perhaps suffices and helps to keep
 *                            the track of how this script works".
 * 
 * Instructions for playing the test:
 * 
 * 0) Load this script with GP.
 * 1) Set some variable to zero, let us say "x":
 
        x=0;

 * 2) Repeat three, four or more times the following line:

        A207324flattenedMatricesByRows(x++,1);
 
 * And this is it. Compare the counters shown there and their possible interpretations.
 * 
*/

/* Additional aid routine not present in other versions
*/
yesno(x)={my(y=1,m);while((m=prod(k=1,y+1,k))<x,y++);if(m==x,"A factorial. Isn't it?","Hmm!...")};

/* The inherited code.
*/
matx2Lst(M)={ 
  my(ans:list);
  ans=listcreate();
  for(i=1,#M[,1],
    for(j=1,#M[1,],
      listput(ans,M[i,j])
    )
  );
  ans
}
U(a,b)=if(b,b,a);
F(m,z,y,x)=if(z,(x==y),(m+1==x+y));

/* A minor edit for supporting the counters.
*/
A207324flattenedMatricesByRows(n,just=0)={
  my(z,m=1,q,r,t,B,a0:list,a1:list,a2:list,ok1,ok2);
  a0=matx2Lst(matrix(1,1,i,j,1));
  a2=listcreate();
  while(m<=n,if(m>1,
    B=matrix(m!,m);
    q=0;
    z=0;
    for(i=1,#B[,1],
      for(j=1,#B[1,],
	B[i,j]=m*F(m,z,U(m,i%m),j);
        q++;
      );
      if(q==m^2,q=0;z=!z);
    );
    a1=matx2Lst(B); /* Here must be n! non-zero elements */
    r=0;
    t=1;
    ok1=0;
    ok2=2; /* The first and the last elements are missing below (by construction). Therefore initial condition is +2 */
    for(u=1,(m-1)!,
      r++;
      for(v=1,m,
        for(q=(r-1)*(m-1)+1,r*(m-1),
          while(a1[t],
            t++;
            ok2++ /* When finished it should have reached: n!*n */ 
          );
          a1[t]=a0[q];
          ok1++ /* When finished it should have reached: n!*(n-1) */
        );
      );
    );
    a0=a1;
    );
  m++;
  for(k=1,#a0,listput(a2,a0[k]));
  );
  
  /* The difference (ok2-ok1) counts the items skipped in the diagonals (from there the name sieve-completion) */
  
  print("\nn="n"; ok1:"ok1" ok2:"ok2" differs by:"ok2-ok1" "yesno(ok2-ok1)"\n\n");
  
/*
 * Returns void due the demonstrative purpose of this version!.
 * 
*/
}



/* Sample output:
 
? x=0;

? A207324flattenedMatricesByRows(x,1)

n=0; ok1:0 ok2:0 differs by:0 Hmm!...


? A207324flattenedMatricesByRows(x++,1)

n=1; ok1:0 ok2:0 differs by:0 Hmm!...


? A207324flattenedMatricesByRows(x++,1)

n=2; ok1:2 ok2:5 differs by:3 Hmm!...


? A207324flattenedMatricesByRows(x++,1)

n=3; ok1:12 ok2:18 differs by:6 A factorial. Isn't it?


? A207324flattenedMatricesByRows(x++,1)

n=4; ok1:72 ok2:96 differs by:24 A factorial. Isn't it?


? A207324flattenedMatricesByRows(x++,1)

n=5; ok1:480 ok2:600 differs by:120 A factorial. Isn't it?


? A207324flattenedMatricesByRows(x++,1)

n=6; ok1:3600 ok2:4320 differs by:720 A factorial. Isn't it?


? A207324flattenedMatricesByRows(x++,1)

n=7; ok1:30240 ok2:35280 differs by:5040 A factorial. Isn't it?


? A207324flattenedMatricesByRows(x++,1)

n=8; ok1:282240 ok2:322560 differs by:40320 A factorial. Isn't it?

*/